package com.example.spring_test2.service;

import com.example.spring_test2.dao.productDao;
import com.example.spring_test2.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class productService {
    @Autowired
    productDao productdao;

    public List<Product> getAllProduct(){
        return productdao.getAllProduct();
    }
    public Product getProductById(long id){
        return productdao.getProductById(id);
    }
    public void addProduct(Product product){
        if(isExisting(product.getId()))
        {
            System.out.println("该商品id已存在 请选择更改商品信息");
            return;
        }
        productdao.addProduct(product);
    }
    public boolean deleteProduct(long id){
        if(!isExisting(id))
        {
            System.out.println("该商品不存在 请重新操作");
            return false;
        }
        productdao.deleteProduct(id);
        return true;
    }
    public boolean updateProduct(Product product){
        if(!isExisting(product.getId()))
        {
            System.out.println("该商品不存在 请重新操作");
            return false;
        }
        productdao.updateProduct(product);
        return true;
    }

    public boolean isExisting(long id) {return getProductById(id) != null;}

}
